package mx.com.sinformex.simbel.db.eos;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;


import org.apache.tapestry5.beaneditor.NonVisual;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.validator.NotNull;

/**
 * 
 * @author Oscar Ivan Hernandez Ventura [ oscar at sinformex dot com dot mx]
 *
 * @version $ Id : Report.java , v 1.0 08/05/2009 angellore $
 */

@Entity(name="report")
public class Report {
	
	public static final String TYPE_CSV = "csv";
	public static final String TYPE_XLS = "xls";
	public static final String TYPE_PDF = "pdv";
	public static final String TYPE_JASPER = "jasper";
	
	public static final int ROWS_PER_PAGE = 10;

	private Integer reportId;
	private String name;
	private String description;
	private boolean xls;
	private boolean pdf;
	private String query;
	private String datasourceName;
	private DataSource datasource;
	private List<ReportParam> params;
	private String jasperReportFileName;
	
	@Id
	@Column(name="report_id")
	@GeneratedValue(strategy=GenerationType.AUTO)
	@NonVisual
	public Integer getReportId() {
		return reportId;
	}
	public void setReportId(Integer reportId) {
		this.reportId = reportId;
	}
	
	@Column(name="name")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name="description")
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	@Column(name="xls")
	public boolean isXls() {
		return xls;
	}
	public void setXls(boolean xls) {
		this.xls = xls;
	}
	
	@Column(name="pdf")
	public boolean isPdf() {
		return pdf;
	}
	public void setPdf(boolean pdf) {
		this.pdf = pdf;
	}
	
	@Column(name="query")
	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	
	@Column(name="datasource_name")
	public String getDatasourceName() {
		return datasourceName;
	}
	public void setDatasourceName(String datasourceName) {
		this.datasourceName = datasourceName;
	}
	
    @NotNull
    @ManyToOne(optional=false)
    @Cascade(value=CascadeType.MERGE)
    @JoinColumn(name="datasource_name",insertable=false, updatable=false)	
	public DataSource getDatasource() {
		return datasource;
	}
	public void setDatasource(DataSource datasource) {
		this.datasource = datasource;
	}
	
	@OneToMany(fetch=FetchType.EAGER)
	@JoinColumn(name = "report_id",unique=true)
	public List<ReportParam> getParams() {
		if(params == null) {
			params = new ArrayList<ReportParam>();
		}
		return params;
	}
	public void setParams(List<ReportParam> params) {
		this.params = params;
	}
	
	public void addToParams(ReportParam param) {
		if(params == null) {
			params = new ArrayList<ReportParam>();
		}
		params.add(param);
	}
	
	public ReportParam getParam(Integer id) {
		if(params == null) {
			params = new ArrayList<ReportParam>();
		}
		for(ReportParam param : params) {
			if(param.getReportParamId() == null) {
				return null;
			}
			if(param.getReportParamId().equals(id)) {
				return param;
			}
		}
		return null;
	}
	@Column(name="jasper_file_name")
	public String getJasperReportFileName() {
		return jasperReportFileName;
	}
	public void setJasperReportFileName(String jasperReportFileName) {
		this.jasperReportFileName = jasperReportFileName;
	}
	public boolean equals (Object obj) {
		if (null == obj) return false;
		if (!(obj instanceof Report)) return false;
		else {
			Report report = (Report) obj;
			if (null == this.getReportId() || null == report.getReportId()) return false;
			else return (this.getReportId().equals(report.getReportId()));
		}
	}
	
	public final String toString() {
		return String.format("[ Id : %d, Name : %s; Hash : %s]", getReportId(), getName(), getClass().hashCode());
	}
		
	
}
